package com.hitgo.soccer.database;

import com.danga.MemCached.SockIOPool;
import com.danga.MemCached.MemCachedClient;

import com.hitgo.soccer.config.CacheConfig;

/*
 * memcached 管理类
 */
public final class CacheFactory
{

	private static SockIOPool pool = null;
	public static MemCachedClient mc = null;

	private final static CacheFactory cache = new CacheFactory();

	public CacheFactory()
	{
	}

	public static CacheFactory instance()
	{
		return cache;
	}

	// 初始化memcache池和客户端连接设置
	public static void init()
	{
		String[] serverlist = CacheConfig.poolList.split(",");
		pool = SockIOPool.getInstance(CacheConfig.poolName);
		pool.setServers(serverlist);

		pool.setInitConn(CacheConfig.initConn);
		pool.setMinConn(CacheConfig.minConn);
		pool.setMaxConn(CacheConfig.maxConn);
		pool.setMaxIdle(CacheConfig.maxIdle);
		pool.setMaintSleep(CacheConfig.maintSleep);
		pool.setAliveCheck(false);// 暂时关闭池中连接是否alive检查
		pool.setSocketConnectTO(CacheConfig.socketConnTo);
		pool.setSocketTO(CacheConfig.socketTo);
		pool.setNagle(CacheConfig.nagle);
		pool.initialize();

		mc = new MemCachedClient(CacheConfig.poolName);
		// mc.setCompressEnable(CacheConfig.compressEnable);
		// mc.setCompressThreshold(CacheConfig.commpressThreshold);
		mc.setSanitizeKeys(CacheConfig.sanitizeKeys);
	}

	// 关闭cache池
	public static void shutdown()
	{
		pool.shutDown();
	}

	public static MemCachedClient getMCClient()
	{
		return mc;
	}
}
